﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RFinance.Domain.Entities;
using RFinance.Domain.Concrete;
using RFinance.Infrastructure;

namespace RFinance.Controllers
{
    public class CategoriaController : Controller
    {
        //
        // GET: /Categoria/

        private GenericRepository<RFinanceContext, Categoria> repository;

        public CategoriaController(GenericRepository<RFinanceContext, Categoria> repo)
        {
            repository = repo;
        }

        public ActionResult Index()
        {
            return View();
        }


        public JsonResult ObterTodas()
        {
            var query = repository.GetAll();
            //var result = new
            //                 {
            //                     total = 100,
            //                     page = 1,
            //                     records = query.Count(),
            //                     rows = (from a in query.AsEnumerable() select new { id = a.CategoriaId, cell = new[] { a.Nome, a.CategoriaId.ToString() } }).ToArray()
            //                 };
            JqGridParser<Categoria> jq = new JqGridParser<Categoria>();
            var result = jq.ObterJsonGrid(query);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create(Categoria categoria)
        {
            if (ModelState.IsValid)
            {
                repository.Add(categoria);
                repository.Save();
                return View("Index", repository.GetAll().ToList());
            }
            else
            {
                return View();
            }
        }

    }
}
